aboutsummaryrefslogtreecommitdiff
path: root/src/routes/user/[user]/+page.svelte
blob: 222e044fc15738218725ea6d5005fb3e6112ee25 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<script lang="ts">
	import ParallaxImage from '../../../lib/Image/ParallaxImage.svelte';
	import { typeSchedule, type ParseResult } from '$lib/Hololive/hololive';
	import { user, type User } from '$lib/Data/AniList/user';
	import HeadTitle from '$lib/Home/HeadTitle.svelte';
	import Message from '$lib/Loading/Message.svelte';
	import { estimatedDayReading } from '$lib/Media/Manga/time';
	import Skeleton from '$lib/Loading/Skeleton.svelte';
	import root from '$lib/Utility/root.js';
	import locale from '$stores/locale.js';
	import { onMount } from 'svelte';
	import authorisedUsers from '$lib/Data/Static/authorised.json';
	import tooltip from '$lib/Tooltip/tooltip';
	import AnimeRateLimited from '$lib/Error/AnimeRateLimited.svelte';
	import identity from '$stores/identity';
	import SettingHint from '$lib/Settings/SettingHint.svelte';
	import proxy from '$lib/Utility/proxy';
	import { parseScheduleHtml } from '$lib/Data/hololive';
	import type { UserPreferences } from '$lib/Database/userPreferences.js';
	import SvelteMarkdown from 'svelte-markdown';
	import MarkdownLink from '$lib/MarkdownLink.svelte';
	import LinkedTooltip from '$lib/Tooltip/LinkedTooltip.svelte';

	export let data;

	let userData: User | undefined = undefined;
	let error = false;
	let schedule: ParseResult | undefined = undefined;
	let preferences: UserPreferences | undefined = undefined;

	$: displayBadges = (username: string, badges: number | string) =>
		$locale({
			values: {
				badges: badges,
				username
			}
		}).user.profile.badges;

	onMount(() => {
		user(data.username, /^\d+$/.test(data.username))
			.then((profile) => (userData = profile))
			.catch(() => (error = true));
	});

	onMount(async () => {
		schedule = typeSchedule(
			parseScheduleHtml(
				await (
					await fetch(proxy('https://schedule.hololive.tv'), {
						headers: {
							Cookie: 'timezone=Asia/Tokyo'
						}
					})
				).text()
			)
		);
	});

	$: if (userData) {
		fetch(root(`/api/preferences?id=${userData.id}`))
			.then((rawPreferences) => rawPreferences.json())
			.then((JSONpreferences) => (preferences = JSONpreferences));
	}

	const getBadgeWallCSS = () =>
		(document.getElementById('badgeWallCSS') as HTMLTextAreaElement).value;

	const getBiography = () =>
		(document.getElementById('biography') as HTMLTextAreaElement).value.slice(0, 3000);

	// 8.5827814569536423841e0
</script>

<HeadTitle route={`${data.username}'s Profile`} path={`/user/${data.username}`} />

{#if error}
	<AnimeRateLimited>
		<a href={`https://anilist.co/user/${data.username}`} target="_blank">@{data.username}</a>'s
		profile could not be loaded.
	</AnimeRateLimited>
{:else}
	{#if userData === null}
		<Message slot withReload>
			<p>
				Could not load user profile for <a
					href={`https://anilist.co/user/${data.username}`}
					target="_blank">@{data.username}</a
				>.
			</p>
		</Message>
	{:else if userData === undefined}
		<Skeleton card={false} bigCard count={1} height="224px" />

		<Message message="Loading user profile ..." />
	{:else}
		<div class="card card-small">
			<div
				class="card user-grid"
				style={`background-image: ${
					userData ? `url(${userData.bannerImage})` : 'none'
				}; padding: 0;`}
			>
				{#if userData}
					<img src={userData.bannerImage} alt="" class="cover-image" />
				{/if}

				<div class="card user-grid-content">
					<div class="user-grid-avatar">
						<LinkedTooltip content={`${userData.name}`} id="avatar" pin="avatar" relative>
							<a href={`https://anilist.co/user/${userData.name}`} target="_blank">
								<ParallaxImage
									source={userData.avatar.large}
									alternativeText=""
									style="border-radius: 8px; width: 6.5em;"
								/>
							</a>
						</LinkedTooltip>
					</div>

					<div class="user-grid-rest">
						<p>
							<a
								href={`https://anilist.co/user/${userData.name}`}
								target="_blank"
								title={String(userData.id)}
								use:tooltip
							>
								@{userData.name}
							</a>
							{#if userData && authorisedUsers.includes(userData.id)}
								&#8204;
								<button class="unclickable-button button-badge badge-rainbow">Owner</button>
							{/if}
							<span class="click-item separator opaque">•</span>
							<a href={root(`/user/${userData.name}/badges`)}>Badge Wall</a>
						</p>

						{#if schedule && preferences && preferences.biography && preferences.biography.length > 0}
							<SvelteMarkdown
								source={preferences.biography}
								renderers={{
									link: MarkdownLink
								}}
							/>
						{/if}

						{$locale({
							values: {
								username: data.username,
								anime: (userData.statistics.anime.minutesWatched / 60 / 24).toFixed(1),
								manga: estimatedDayReading(userData.statistics.manga.chaptersRead).toFixed(1)
							}
						}).user.profile.statistics}

						{#if schedule && preferences && preferences.biography && preferences.biography.length > 0}
							<br />
						{:else}
							<p />
						{/if}

						{#await fetch(root(`/api/badges?id=${userData.id}`))}
							{displayBadges(userData.name, '...')}
						{:then badges}
							{#await badges.json()}
								{displayBadges(userData.name, '...')}
							{:then badges}
								{displayBadges(userData.name, badges.length)}
							{:catch}
								{displayBadges(userData.name, '?')}
							{/await}
						{:catch}
							{displayBadges(userData.name, '?')}
						{/await}
					</div>
				</div>
			</div>
		</div>
	{/if}

	{#if schedule && preferences && preferences.pinned_hololive_streams.length > 0}
		<p />

		<div class="card">
			<div class="hololive-badges">
				{#each preferences.pinned_hololive_streams as stream, index}
					{@const avatar = schedule.dict[stream]}

					{#if avatar}
						<LinkedTooltip
							content={stream}
							id={`hololive-badge-${index}`}
							pin={`hololive-badge-${index}`}
							relative
						>
							<a href={root(`/hololive/${encodeURIComponent(stream)}`)}>
								<div class="user-grid-hololive-badges">
									<ParallaxImage source={avatar} alternativeText="Avatar" />
								</div>
							</a>
						</LinkedTooltip>
					{/if}
				{/each}
			</div>
		</div>
	{/if}

	{#if preferences && userData && userData.id === $identity.id}
		<p />

		<details open>
			<summary>{$locale().user.preferences.title}</summary>

			<input
				type="checkbox"
				on:change={() => {
					if (userData)
						fetch(root(`/api/preferences?id=${userData.id}&toggleHideMissingBadges`), {
							method: 'PUT'
						});
				}}
				checked={preferences.hide_missing_badges}
			/>
			{$locale().user.preferences.hideMissingBadges.title}
			<SettingHint lineBreak>{$locale().user.preferences.hideMissingBadges.hint}</SettingHint>

			<p />

			Biography

			<button
				on:click={() => {
					if (userData)
						fetch(root(`/api/preferences?id=${userData.id}&biography`), {
							method: 'PUT',
							body: getBiography()
						}).then(() =>
							fetch(root(`/api/preferences?id=${userData?.id}`))
								.then((rawPreferences) => rawPreferences.json())
								.then((JSONpreferences) => (preferences = JSONpreferences))
						);
				}}>Save</button
			>
			<textarea
				value={preferences.biography}
				rows="5"
				cols="100"
				id="biography"
				placeholder="Markdown supported!"
			/>

			<p />

			Badge Wall Custom CSS

			<button
				on:click={() => {
					if (userData)
						fetch(root(`/api/preferences?id=${userData.id}&badgeWallCSS`), {
							method: 'PUT',
							body: getBadgeWallCSS()
						});
				}}>Save</button
			>
			<textarea
				value={preferences.badge_wall_css}
				rows="10"
				cols="100"
				id="badgeWallCSS"
				placeholder="/* Use classes and IDs such as .badges, #badges, .badge, or standard elements like body and details, or anything, as long as it's valid CSS! */"
			/>
		</details>
	{/if}
{/if}

<style lang="scss">
	.user-grid-content {
		display: flex;
		flex-wrap: wrap;
		column-gap: 1.5em;
		background-color: rgba(0, 0, 0, 0.468);
		color: #d8d8d8;
		border-top-left-radius: 0;
		border-top-right-radius: 0;
	}

	.user-grid-avatar {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.click-item {
		margin: 0 0.625rem;
	}

	.user-grid {
		background-size: cover;
		background-position: center;
		background-repeat: no-repeat;
	}

	.cover-image {
		visibility: hidden;
		height: 4.5em;
	}

	.user-grid-hololive-badges {
		$transitionDuration: 0.45s;
		$transition: transform $transitionDuration ease, box-shadow $transitionDuration ease;
		$size: 5.25em;

		border-radius: 8px;
		width: $size;
		height: $size;
		object-fit: cover;
		transition: $transition;
		box-shadow: rgba(0, 0, 11, 0.1) 0px 7px 29px 0px;
		overflow: hidden;

		&:hover {
			z-index: 2;
			transition: $transition;
			transform: scale(1.15);
			box-shadow: 0 1.5px 9px var(--base01), 0 0 0 4px var(--base02), 0 4px 30px var(--base01);
		}
	}

	.hololive-badges {
		display: flex;
		gap: 1rem;
		border-radius: 8px;
		justify-content: space-around;
		flex-wrap: wrap;
	}

	.separator {
		color: var(--base04);
	}

	.user-grid-rest {
		flex: 1;
	}
</style>